package scct.yanzhengxin.dao;

import scct.yanzhengxin.model.User;
import scct.yanzhengxin.utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

public class UserDao {
    /**
     * @param name 用户名
     * @param pass 密码
     * @return 用户对象
     */
    public User login(String name, String pass) {
        User user = null;
//        获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where username=? and password=?";
//        预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
//            查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                String mobile = rs.getString("mobile");
                user = new User(id, username, password,  gender,age, mobile);

            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public boolean updateUser(int id, String password, String gender,
                              int age, String mobile){
        int count=0;
        Connection conn=DBUtils.getConnection() ;
        String update_sql="update sm_user set mobile=?,age=?,gender=?,password=? where id ";
        try {
            PreparedStatement pstmt= conn.prepareStatement(update_sql);
            pstmt.setString(1,mobile);
            pstmt.setInt(2,age);
            pstmt.setString(3,gender);
            pstmt.setString(4,password);

            count= pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  count>0;

    }



    public  boolean isDupname(String name){
        int count =0;
        Connection conn=DBUtils.getConnection();
        String sql="select * from sm_user where username=? ";
        try {
            PreparedStatement pstmt= conn.prepareStatement(sql);
            pstmt.setString(1,name);
            ResultSet rs= pstmt.executeQuery();
            if (rs.next()){
               count=1;
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
return  count>0;
    }

    public List<User> getAllUser() {
        Connection conn = DBUtils.getConnection();
        List<User> users = new ArrayList<>();

        String sql = "select * from sm_user";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                String mobile = rs.getString("mobile");

                users.add(new User(id, username, password, gender,age, mobile));

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return users;

    }
    public boolean addUser(String username,String password,String gender,int age,String mobile){
        int result=0;
        Connection conn=DBUtils.getConnection();
        String insert_sql="insert into sm_user values(null,?,?,?,?,?)";
        try {
            PreparedStatement pstmt= conn.prepareStatement(insert_sql);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            pstmt.setString(3,gender);
            pstmt.setInt(4,age);
            pstmt.setString(5,mobile);
            result=pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return  result==1;
    }

    public  User getUserById(int id){
        User user=null;
        Connection conn=DBUtils.getConnection();
        String sql="select * from sm_user where id=?";
        try {
            PreparedStatement pstmt= conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs= pstmt.executeQuery();
            if (rs.next()) {
                String username= rs.getString("username");
                String password= rs.getString("password");
                String mobile= rs.getString("mobile");
                String gender= rs.getString("gender");
                int age= rs.getInt("age");
                user=new User(id,username,password,gender,age,mobile);
            }
            DBUtils.closeConnection(conn);
        }catch (SQLException e){
            throw  new RuntimeException();
        }
        return user;
    }
}

